package com.cszy.yydqbfq.dialog

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cszy.yydqbfq.R
import com.cszy.yydqbfq.adapter.SongDeailPopAdapter
import com.cszy.yydqbfq.ui.activity.PlayDetailActivity
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.lzx.starrysky.SongInfo

class BottomPopupWithRecycler(
    private val mContext: Context,
    private val dataList: MutableList<SongInfo>
) {
    private var newGromoreFeedAd: NewGroMoreFeedAdManager? = null
    private val popupView = LayoutInflater.from(mContext).inflate(R.layout.pop_bottom, null)
    private val popupWindow = PopupWindow(
        popupView,
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    ).apply {
        isOutsideTouchable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        animationStyle = R.style.PopupAnimation
    }

    fun show(anchorView: View, position: Int) {
        val recyclerView = popupView.findViewById<RecyclerView>(R.id.recy_pop_list)
        val ll_empty = popupView.findViewById<LinearLayout>(R.id.ll_empty)
        val tv_empty = popupView.findViewById<TextView>(R.id.tv_empty)
        tv_empty.setText("请先播放歌单~")
        if (dataList.isEmpty()) {
            Log.e("qin", "dataList.isEmpty()")
            ll_empty.visibility = View.VISIBLE

        } else {
            Log.e("qin", "dataList.isNotEmpty()")
            ll_empty.visibility = View.GONE      // 隐藏空布局
        }
        val mAdapter = SongDeailPopAdapter().apply {
            submitList(dataList.toMutableList())
            setPosition(position)
        }
        recyclerView.layoutManager = LinearLayoutManager(anchorView.context)
        recyclerView.adapter = mAdapter


        mAdapter.setOnItemClickListener { adapter, view, position ->
            mAdapter.setPosition(position)
            val intent = Intent(mContext, PlayDetailActivity::class.java).apply {
                putParcelableArrayListExtra("MUSIC_LIST", ArrayList(dataList))
                putExtra("CURRENT_POSITION", position)
            }
            mContext.startActivity(intent)
        }

        var iv_xiala = popupView.findViewById<View>(R.id.iv_xiala)
        iv_xiala.setOnClickListener {
            dismiss()
        }
        //信息流
        var frame_gedan = popupView.findViewById<FrameLayout>(R.id.frame_gedan)
        newGromoreFeedAd = NewGroMoreFeedAdManager(
            mContext as Activity,
            BaseAdConstants.FEEDAD_Id,
            frame_gedan,
            46
        )
        newGromoreFeedAd?.loadAd()
        popupWindow.showAtLocation(anchorView, Gravity.BOTTOM, 0, 0)
    }

    fun dismiss() {
        popupWindow.dismiss()
    }
}